package com.shuda.db.user.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuda.db.user.entity.User;
import com.shuda.db.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDao {
    
    @Autowired
    private UserMapper userMapper;
    
    public User getById(Long id) {
        return userMapper.selectById(id);
    }
    
    public User getByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return userMapper.selectOne(wrapper);
    }
    
    public List<User> getList(Page<User> page) {
        return userMapper.selectPage(page, null).getRecords();
    }
    
    public int insert(User user) {
        return userMapper.insert(user);
    }
    
    public int update(User user) {
        return userMapper.updateById(user);
    }
    
    public int delete(Long id) {
        return userMapper.deleteById(id);
    }
    
    public boolean existsByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return userMapper.selectCount(wrapper) > 0;
    }
}